package com.bjwl.web.contrlller.branchstore;

import com.bjwl.pojo.*;
import com.bjwl.pojo.dto.BusinessBrDto;
import com.bjwl.service.BusinessService;
import com.bjwl.service.CardService;
import com.bjwl.service.DailyService;
import com.bjwl.service.impl.BusinessServiceImpl;
import com.bjwl.utils.PageBean;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.stereotype.Controller;
import org.springframework.stereotype.Repository;
import org.springframework.ui.Model;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.context.request.WebRequest;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author: czc
 * @Date:Create：in 2020/4/14 13:12
 * @Description:
 * @Modified By：
 * @Version: 1.0
 */
@Controller
@RequestMapping("/bus")
public class BusinessController {

    @Autowired
    private BusinessService businessService;
    @Autowired
    private DailyService dailyService;
    @Autowired
    private CardService cardService;

    //展示所有业务信息
    @RequestMapping("/findBusineseBybStoreMinId")
    public String findBusineseBybStoreMinId(HttpSession httpSession,Integer branchstoreid){
        List<Business> businessesList = businessService.byBsIdFindBusinessC(branchstoreid);
        httpSession.setAttribute("businessesList",businessesList);
        System.out.println(businessesList);
        return "redirect:/br/business-list";
    }

    @RequestMapping(value = "/updateShowBusinese",method = RequestMethod.GET)
    public String updateShowBusinese(HttpSession httpSession,@RequestParam("buid") Integer buid){
        Business businesss = businessService.updateShowBusinese(buid);
        httpSession.setAttribute("businesss",businesss);
        return "branch_oa/business-edit";
    }

    //根据当前id进行业务信息修改
    @RequestMapping("/updateBusineseByid")
    public @ResponseBody  Boolean updateBusineseByid(Business business){
        businessService.updateBusinessByid(business);



        return true;
    }

    //根据当前id进行业务信息删除
    @RequestMapping("/deleteBusinese")
    public @ResponseBody Boolean deleteBusinese(Integer buid){

        Business business = businessService.updateShowBusinese(buid);


        businessService.deleteBusinese(buid);
        return true;
    }

    //添加业务信息
    @RequestMapping("/addBusiness")
    public @ResponseBody Boolean addBusiness(Business business,HttpSession httpSession){
        business.setInserttime(new Date());
        Integer hbsstate = (Integer) httpSession.getAttribute("hbsstate");
        business.setBsstate(hbsstate);
        businessService.addBusiness(business);

      /*  httpSession.removeAttribute("hbsstate");*/

        return true;
    }

    //查询当前分店业务数
    public Integer finBusinessCount  (Integer bsid) {
        Integer businessConunt = businessService.finBusinessCount(bsid);
        return businessConunt;
    }

    //根据当前管理员的分店id查询派单信息
    @RequestMapping("/findBusinessBS")
    public String findBusinessBS(HttpSession httpSession, HttpServletRequest request, Integer bsstate, Integer pageNum , String bstarttime , String bendtime, Integer bopid , String bopvalue) {
        if(pageNum == null){
            pageNum =1;
        }

        if(bsstate==0) {
            abc(httpSession,request,pageNum,bstarttime,bendtime,bopid,bopvalue,0);
            return "branch_oa/business-list";
        }else{
            abc(httpSession,request,pageNum,bstarttime,bendtime,bopid,bopvalue,1);
            return "branch_oa/nanny-business-list";
        }

    }

    public void abc(HttpSession httpSession, HttpServletRequest request,  Integer pageNum , String bstarttime , String bendtime, Integer bopid , String bopvalue,Integer bsstate){
        Bsadmin bsadmin1 = (Bsadmin) httpSession.getAttribute("bsadmin1");
        Integer branchstoreid = bsadmin1.getBsid();
        PageBean<Business> businessList = businessService.findBusinessBS(pageNum,branchstoreid,bstarttime,bendtime,bopid,bopvalue,bsstate);
        request.setAttribute("businessList", businessList);
        BusinessBrDto businessBrDto = new BusinessBrDto(bstarttime,bendtime,bopid,bopvalue,bsstate);
        request.setAttribute("businessBrDto",businessBrDto);
        httpSession.setAttribute("hbsstate",bsstate);
    }
    //*********************************************************************************************************



}
